package com.base.caselib;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.amap.api.location.AMapLocation;
import com.arvin.imagepicker.bean.ImageItem;
import com.base.caselib.bean.CarType;
import com.base.caselib.bean.ComFileBean;
import com.base.caselib.bean.PeopleInfoBean;
import com.base.caselib.bean.RegPeopleBean;
import com.base.caselib.databinding.ActivityRegistpersonBinding;
import com.base.caselib.presenter.RegistPersonPresenter;
import com.base.caselib.view.RegistPersonView;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.HistoryText;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.CommonDialog;
import com.base.commonlib.widget.SingleSearchDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2020/2/5.
 */
@Route(path = "/careg/registperson")
public class RegistPersonActivity extends CBaseBarMvpActivity<RegistPersonPresenter> implements RegistPersonView{
    private ActivityRegistpersonBinding mBinding;
    private PeopleInfoBean mBean = new PeopleInfoBean();
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_registperson;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityRegistpersonBinding) mRootBinding;
        mBinding.setEntity(mBean);
        mPresenter = new RegistPersonPresenter(this,this);
        mPresenter.start();
        mPresenter.startLocationForLl();
        setBackNavigationTip(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWaringDialog.show();
            }
        });
        setBarTitle("人员登记(社区)");
        mBinding.history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SingleSearchDialog<HistoryText> dialog = new SingleSearchDialog<HistoryText>(mContext, "历史地址", BaseApp.gethistoryText(mPresenter.mUser.getUserId(),10), new SingleSearchDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {
                        HistoryText bean = (HistoryText) o;
                        mBinding.checkAddress.setText(bean.getText());
                    }
                });
                dialog.show();
            }
        });


    }

    @Override
    protected void start() {
        mBinding.drivingFirstTimeN.setChecked(true);
        mBinding.fareN.setChecked(true);
        mBinding.localY.setChecked(true);

        mBinding.phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b){
                    if (!TextUtils.isEmpty(mBinding.phone.getText())){
                        mPresenter.vaildPhone(mBinding.phone.getText().toString());
                    }
                }
            }
        });
        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonDialog commonDialog = new CommonDialog(mContext, "确认登记?");
                commonDialog.setOnPosClick(new CommonDialog.OnPosClick() {
                    @Override
                    public void onClick() {
                        List<String> strList = new ArrayList<>();
                        if (mBinding.perPic.getPics()!=null){
                            for (ImageItem item : mBinding.perPic.getPics()){
                                strList.add(item.path);
                            }
                        }
                        mBinding.getEntity().setPhoteFiles(strList);

                        List<String> strList1 = new ArrayList<>();
                        if (mBinding.cardPic.getPics()!=null){
                            for (ImageItem item : mBinding.cardPic.getPics()){
                                strList1.add(item.path);
                            }
                        }
                        mBinding.getEntity().setSfzFiles(strList1);

                        if (mBinding.drivingFirstTimeY.isChecked()){
                            mBinding.getEntity().setAreaContact(1);
                        }else {
                            mBinding.getEntity().setAreaContact(0);
                        }
                        if (mBinding.fareY.isChecked()){
                            mBinding.getEntity().setFebrileState(1);
                        }else {
                            mBinding.getEntity().setFebrileState(0);
                        }
                        if (mBinding.localY.isChecked()){
                            mBinding.getEntity().setIsLocalHousehold(1);
                        }else {
                            mBinding.getEntity().setIsLocalHousehold(0);
                        }
                        mPresenter.up(mBinding.getEntity());
                    }
                });
                commonDialog.show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

    @Override
    public void returnLocation(AMapLocation location) {
        if (location.getAddress().startsWith("江苏省苏州市")){
            mBinding.checkAddress.setText(location.getAddress().substring(6,location.getAddress().length()));
        }else {
            mBinding.checkAddress.setText(location.getAddress());
        }
    }

    @Override
    public void returnInfo(RegPeopleBean bean) {
        mBinding.checkAddress.setText(bean.getCheckPoint());
        mBinding.phone.setText(bean.getPhone());
        mBinding.address.setText(bean.getAddress());
        if (bean.getIsLocalHousehold()==1){
            mBinding.localY.setChecked(true);
        }else {
            mBinding.localN.setChecked(true);
        }
        if (bean.getAreaContact()==1){
            mBinding.drivingFirstTimeY.setChecked(true);
        }else {
            mBinding.drivingFirstTimeN.setChecked(true);
        }
        if (bean.getFebrileState()==1){
            mBinding.fareY.setChecked(true);
        }else {
            mBinding.fareN.setChecked(true);
        }
    }
}
